<template>
  <view>
    <public-header title="库存预警"></public-header>
    <view class="retail-segmented bg-white">
      <uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text"
        activeColor="#38C09E"></uni-segmented-control>
      <view class="content retail-segmented-content bg-white">
        <view v-show="current === 0">
          <bUpdate :submit-on-confirm="true" :selection="false" ref="inventoryUpdate" :page="config.page"
            :editorType="type" :inPageType="'batch-update'" :config="config" @on-list-item-checked="onItemChecked"
            @save="onSave" @list-load="listLoad" :condition="condition"></bUpdate>
        </view>
        <view v-show="current === 1" style="height: 100%;">
          <rList ref="rList" :page="warConfig.page" :config="warConfig" @on-grid-button="onGridButton"
            @on-list-item-checked="onItemChecked" :condition="condition" style="height: 100%;">
            <template v-slot:rListItem="itemValue">
            </template>
            <template v-slot:rLayoutRight>
            </template>
          </rList>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import bUpdate from '@/components/g-batch-update/g-batch-update.vue'
  import rList from '@/components/retail-list/retail-list.vue'
  import lockscreen from '@/common/mixins/lockscreen.js'
  export default {
    mixins: [lockscreen],
    data() {
      return {
        items: ['库存预警设置', '库存预警商品'],
        current: 0,
        service: "", // 页面service
        type: "update", // 页面编辑模式
        config: {
          serviceName: "srvretail_stock_warning_select",
          listLayout: "batchList", // listAndDetail listAndFilter  list  batchList   srvretail_stock_early_warning_select
          defaultData: "service-loading", // batch-select   service-loading
          detailPath: "simple", //  simple  基础的； 或者配置页面路由 路由格式待定
          page: {
            pageNo: 1,
            rownumber: 10
          },
          listHeaders: [''],
          childService: "srvretail_stock_warning_select",
          childKeyCol: "", // 子表数据的主键
          tabsType: "custom", //
          titleText: "返回",
          title: "",
          popupColumns: ["min_stock", "max_stock"],
          custTabs: [{
            "colName": "classify_path",
            // "type": "input",
            // "inputType":"String",
            "type": "tree",
            "inputType": "tree",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "商品分类",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "goods_barcode",
            "type": "input",
            "inputType": "String",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "商品条码",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "goods_name",
            "type": "input",
            "inputType": "String",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "商品名称",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "supplier",
            // "type": "input",
            // "inputType": "String",
            "type": "select",
            "inputType": "fk",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "供应商",
            placeholder:"请选择",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": [],
            "appendSearchButton": true,
            options: {
              "serviceName": "srvretail_supplier_info_select",
              "refed_col": "supplier_name",
              "key_disp_col": "supplier_name",
              "conditions": [],
              "show_as_pair": false,
              "is_append_disp": true
            },
          }],
          tabs: {
            layout: 'inline', // 'inline'   block
          }
        },
        warConfig: {
          serviceName: "srvretail_stock_early_warning_select",
          listLayout: "listAndFilter", // listAndDetail listAndFilter  list  batchList   srvretail_stock_early_warning_select
          defaultData: "service-loading", // batch-select   service-loading
          detailPath: "simple", //  simple  基础的； 或者配置页面路由 路由格式待定
          page: {
            pageNo: 1,
            rownumber: 10
          },
          listHeaders: [''],
          childService: "srvretail_stock_warning_select",
          childKeyCol: "", // 子表数据的主键
          tabsType: "custom", //
          titleText: "返回",
          title: "",
          searchValKey: [],
          popupColumns: [],
          custTabs: [{
            "colName": "classify_path",
            "type": "input",
            // "inputType":"String",
            "type": "tree",
            "inputType": "tree",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "商品分类",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "goods_barcode",
            "type": "input",
            "inputType": "String",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "商品条码",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "goods_name",
            "type": "input",
            "inputType": "String",
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "商品名称",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": []
          }, {
            "colName": "supplier",
            // "type": "input",
            // "inputType": "String",
            "type": "select",
            "inputType": "fk",
            options: {
              "serviceName": "srvretail_supplier_info_select",
              "refed_col": "supplier_name",
              "key_disp_col": "supplier_name",
              "conditions": [],
              "show_as_pair": false,
              "is_append_disp": true
            },
            "parent": "",
            "is_count": false,
            "show_expr": {},
            "label": "供应商",
            "showLabel": true,
            "default": "",
            "colspan": 24,
            "showAllTag": false,
            "tags": [],
            "appendSearchButton": true,
            placeholder:"请选择"
          }],
          tabs: {
            layout: 'inline', // 'inline'   block
          }
        },
        condition: [],
        list: []
      };
    },
    components: {
      bUpdate,
      rList
    },
    created() {

      this.config.custTabs[0].option_list_v2 = (this.$store?.state?.$classifyList || []).map(item => {
        item.text = item.classify_name;
        item.value = item.path;
        return item
      })

      this.warConfig.custTabs[0].option_list_v2 = (this.$store?.state?.$classifyList || []).map(item => {
        item.text = item.classify_name;
        item.value = item.path;
        return item
      })
    },

    methods: {
      selectSupplier() {
        const url = '/retail/select/srvretail_supplier_info_select'
        const req = {
          "serviceName": "srvretail_supplier_info_select",
          "colNames": ["*"],
        }
        this.$http.post(url, req).then(res => {
          if (Array.isArray(res?.data?.data) && res?.data?.data.length > 0) {
            this.config.custTabs[this.config.custTabs.length - 1].options = res.data.data.map(item => {
              item.text = item.supplier_name;
              item.value = item.supplier_name;
              return item
            })
            this.warConfig.custTabs[this.warConfig.custTabs.length - 1].options = res.data.data.map(item => {
              item.text = item.supplier_name;
              item.value = item.supplier_name;
              return item
            })
          }
        })
      },
      listLoad(e = []) {
        this.list = this.deepClone(e)
      },
      saveStock(req) {
        const url = `/retail/operate/srvretail_stock_warning_update`
        this.$http.post(url, req).then(res => {
          if (res?.data?.state == 'SUCCESS') {
            if (this.current == 0) {
              this.$refs.inventoryUpdate.getTableDatas()
            }
          }
          if (res?.data?.resultMessage) {
            uni.showToast({
              title: res.data.resultMessage,
              icon: 'none'
            })
          }
        })
      },
      onSave(list) {
        if (list.length > 0 && list.length === this.list.length) {
          let arr = []
          list.forEach((item, index) => {
            let obj = {}
            if (this.list[index].min_stock !== item.min_stock) {
              obj.min_stock = item.min_stock
            }
            if (this.list[index].max_stock !== item.max_stock) {
              obj.max_stock = item.max_stock
            }
            if (obj.min_stock || obj.min_stock === 0 || obj.max_stock) {
              obj.id = item.id
              arr.push(obj)
            }
          })

          if (Array.isArray(arr) && arr.length > 0) {
            arr = arr.map(item => {
              return {
                "serviceName": "srvretail_stock_warning_update",
                "data": [{
                  "min_stock": item.min_stock,
                  "max_stock": item.max_stock
                }],
                "condition": [{
                  "colName": "id",
                  "value": item.id,
                  "ruleType": "eq"
                }]
              }
            })
            uni.showModal({
              title: '提示',
              content: '确认操作?',
              success: (res) => {
                if (res.confirm) {
                  this.saveStock(arr)
                }
              }
            })

          } else {
            uni.showToast({
              title: '没有修改的数据',
              icon: 'none'
            })
          }
        } else {
          return
        }
      },
      onGridButton(e) {
        console.log('onGridButton', e);
      },
      onItemChecked(e) {
        console.log('onItemChecked', e);
      },
      onClickItem(e) {
        if (this.current != e.currentIndex) {
          this.current = e.currentIndex;
          if (this.current == 0) {
            this.$refs.inventoryUpdate.getTableDatas()
          } else if (this.current == 1) {
            this.$refs.rList.getTableDatas()
          }
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .retail-list .retail-list-right {
    height: calc(100vh-190rpx) !important;
  }
</style>
